< previous page page_163 next page >

Page 163
' Now strip off the path
LastBackslashPos = InStrRev(ExecName, "\")
If LastBackslashPos = 0 Then
   LastBackslashPos = InStrRev(ExecName, ":")
End If
ExecName = Mid$(ExecName, LastBackslashPos + 1)
The resulting string is shown in Figure S5-3.
0163-01.gif
Figure S5-3:
Contents of the ExecName string after removing the path
How long is the string after this operation? It depends on your point of view. If this string were passed to an API function, it would see a string containing 7 characters followed by a NULL character. But Visual Basic sees things differently. It thinks the string is a 258-character string consisting of the file name "vb6.exe" followed by 251 NULL characters. What happens when you run the following code?
If LCase$(ExecName) = "vb6.exe" Or _
      LCase$(ExecName) = "vb5.exe" Or _
      LCase$(ExecName) = "vb32.exe" Then
      IsThisVB = True
   End If
In the comparison to "vb6.exe", Visual Basic tries to compare a 258-character string with a 7-character string. Naturally, they aren't the same. It is necessary for you to strip off all of the extra NULL characters. One way to do this is shown here:
NullPosition = InStr(ExecName, Chr$(0))
ExecName = Left$(ExecName, NullPosition - 1)
Like many API functions of this type, the GetModuleFileName function returns the number of characters that it loaded into the string. In this example, it would return 10the length of the full path and executable name. We could have used this return value to strip off the NULL characters before removing the path. Both approaches work equally well.
I strongly encourage you to follow the example shown here for debugging your own API problems. Visualizing the contents of variables and their organization in memory can help you resolve many API-related problems.

 
< previous page page_163 next page >